<?php
/**
 * Created by PhpStorm.
 * User: LAKECASEY
 * Date: 2018/12/11
 * Time: 16:52
 */
namespace App\model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Illuminate\Database\Query;

class Misspeople extends Model{
    use  SoftDeletes;
    //绑定表
    protected $table = 'misspeople';

    public $timestamps = true;
    const CREATED_AT = 'create_time';
    const UPDATED_AT = 'update_time';
    /**
     * 应该被调整为日期的属性
     * @var array
     */
    protected $dates = ['deleted_at'];
    protected $dateFormat = 'U';
    /*
     * 添加数据
     */
    public function Addpeople($request)
    {
        //进行入库处理
        DB::beginTransaction();
        try {
            //新增数据
            $addid = $this->insertGetId([
                'miss_name' => $request['miss_name'],
                'miss_sex' => $request['miss_sex'],
                'miss_home' => $request['miss_home'],
                'miss_barth' => $request['miss_barth'],
                'miss_high' => $request['miss_high'],
                'miss_date' => $request['miss_date'],
                'miss_access' => $request['miss_access'],
                'maybe_access' => $request['maybe_access'],
                'money' => $request['miss_money'],
                'find_type' => $request['find_type'],
                'miss_face_text' => $request['miss_face'],
                'miss_experience' => $request['miss_experience'],
                'other_desc' => $request['others'],
                'contact_name' => $request['contact_name'],
                'contact_net' => $request['contact_net'],
                'contact_phone' => $request['contact_phone'],
                'contact_email' => $request['contact_email'],
                'contact_other' => $request['contact_other'],
                'miss_pic' => $request['peope_pic'],
                // 'video_url' => $request['videourl'],
                'code' => $request['code'],
                'find_area' => $request['find_area'],
                'showindex' => 0,
                'is_shelf' => 0,
                'create_time' => time(),
            ]);
            DB::commit();
            return true;
        }catch(\Exception $e){
            DB::rollBack();
            return response()->json($e->getMessage());
        }
    }

    /**
     * 获取所有数据
     * @param $typeid
     * @param $request
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     */
    public function getAlldatas($typeid,$request)
    {
        $search = [];
        if ($typeid != 0)
        {
            $search[] = ['find_type','=',$typeid];
        }
        if (!empty($request['search_text']))
        {
            $search[] = ['miss_name','like',"%{$request['search_text']}%"];
        }
        if (!empty($_COOKIE['citycode']) && is_numeric($_COOKIE['citycode']))
        {
            $search[] = ['find_area','=',$_COOKIE['citycode']];
        }
        $search[] = ['is_shelf','=',1];
        $data = self::leftJoin('china as c',function ($join){
            $join->on("misspeople.find_area",'=','c.Id');
        })->where($search)->where('misspeople.is_shelf','=','1')->orderBy('misspeople.create_time','desc')->select('misspeople.*','c.Name')->paginate(8);
        return $data;
    }

    /**
     * 获取详情
     * @param $id
     * @return array
     */
    public function getDetial($id)
    {
        return self::leftJoin('china as c',function ($join){
            $join->on("misspeople.find_area",'=','c.Id');
        })->where("misspeople.id",'=',$id)->where("misspeople.is_shelf",'=',1)->select('misspeople.*','c.Name')->first();
    }

    /**
     * 获取相同的数据
     * @param $samid
     */
    public function getSame($id,$samid)
    {
        $sam = Misspeople::where('is_shelf','=','1')->where('find_type','=',$samid)->where('id','!=',$id)->limit(5)->get();
        return $sam;
    }

    /**
     * 获取最新寻人消息
     * @return \Illuminate\Database\Eloquent\Collection|static[]
     */
    public function getNewmsg()
    {
        $search = [];
        $search[] = ['is_shelf','=',1];
        if (!empty($_COOKIE['citycode']) && is_numeric($_COOKIE['citycode']))
        {
            $search[] = ['misspeople.find_area','=',$_COOKIE['citycode']];
        }
        return self::leftJoin('china as c',function ($join){
            $join->on("misspeople.find_area",'=','c.Id');
        })->where($search)->orderBy('create_time','desc')->select('misspeople.*','c.Name')->limit(36)->get();
    }

    /**
     * 根据类型去寻找不同的信息
     * @param $find_type
     */
    public function findtype_msg($find_type)
    {
        $search = [];
        $search[] = ['is_shelf','=',1];
        if (!empty($_COOKIE['citycode']) && is_numeric($_COOKIE['citycode']))
        {
            $search[] = ['misspeople.find_area','=',$_COOKIE['citycode']];
        }
        return self::leftJoin('china as c',function ($join){
            $join->on("misspeople.find_area",'=','c.Id');
        })->where($search)->where('misspeople.find_type','=',$find_type)->select('misspeople.*','c.Name')->orderBy('misspeople.create_time','desc')->orderBy('misspeople.first_order','desc')->limit(21)->get();
    }

    /**
     * 首页寻人切换选项
     * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|static[]
     */
    public function findtype_msg_all()
    {
        $search = [];
        $search[] = ['is_shelf','=',1];
        if (!empty($_COOKIE['citycode']) && is_numeric($_COOKIE['citycode']))
        {
            $search[] = ['misspeople.find_area','=',$_COOKIE['citycode']];
        }
        return self::leftJoin('china as c',function ($join){
            $join->on("misspeople.find_area",'=','c.Id');
        })->select('misspeople.*','c.Name')->orderBy('misspeople.create_time','desc')->orderBy('misspeople.first_order','desc')->inRandomOrder()->take(21)->get();
    }
    /**
     * @return \Illuminate\Database\Eloquent\Collection|static[]
     */
    public function getSuggest()
    {
        $search = [];
        $search[] = ['is_shelf','=',1];
        $search[] = ['showindex','=','1'];
        if (!empty($_COOKIE['citycode']) && is_numeric($_COOKIE['citycode']))
        {
            $search[] = ['find_area','=',$_COOKIE['citycode']];
        }
        return Misspeople::where($search)->limit(2)->get();
    }

    /**
     * @return mixed
     */
    public function getRand()
    {
        $search = [];
        $search[] = ['is_shelf','=',1];
        if (!empty($_COOKIE['citycode']) && is_numeric($_COOKIE['citycode']))
        {
            $search[] = ['find_area','=',$_COOKIE['citycode']];
        }
       return DB::table('misspeople')
            ->where($search)
            ->inRandomOrder()
            ->take(8)
            ->get();
    }

    /**
     * @return \Illuminate\Database\Eloquent\Collection|static[]
     */
    public function getHome()
    {
        return Misspeople::where('status','=','1')->where('is_shelf','=',1)->limit(3)->get();
    }

    /**
     * @param $num
     */
    public function getRandhome($num)
    {
        return DB::table('misspeople')
            ->where('is_shelf','=',1)
            ->where('status','=',1)
            ->inRandomOrder()
            ->take($num)
            ->get();
    }

    /**
    *   后台数据添加
    */
    public function AddpeopleAdmin($request)
    {
        //进行入库处理
        DB::beginTransaction();
        try {
            //新增数据
            $addid = $this->insertGetId([
                'miss_name' => $request['miss_name'],
                'miss_sex' => $request['miss_sex'],
                'miss_home' => $request['miss_home'],
                'miss_barth' => $request['miss_barth'],
                'miss_high' => $request['miss_high'],
                'miss_date' => $request['miss_date'],
                'miss_access' => $request['miss_access'],
                'maybe_access' => $request['maybe_access'],
                'money' => $request['miss_money'],
                'find_type' => $request['find_type'],
                'miss_face_text' => $request['miss_face'],
                'miss_experience' => $request['miss_experience'],
                'other_desc' => $request['others'],
                'contact_name' => $request['contact_name'],
                'contact_net' => $request['contact_net'],
                'contact_phone' => $request['contact_phone'],
                'contact_email' => $request['contact_email'],
                'contact_other' => $request['contact_other'],
                'miss_pic' => $request['peope_pic'],
                'first_order' => $request['first_order'],
                // 'video_url' => $request['videourl'],
                'code' => $request['code'],
                'find_area' => $request['find_area'],
                'showindex' => 0,
                'is_shelf' => 0,
                'create_time' => time(),
            ]);
            DB::commit();
            return true;
        }catch(\Exception $e){
            DB::rollBack();
            return response()->json($e->getMessage());
        }
    }
}